package com.apiit.webserviceclient.activities;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.apiit.webserviceclient.R;
import com.apiit.webserviceclient.db.Authentication;
import com.apiit.webserviceclient.db.Authentication_DAO;
import com.apiit.webserviceclient.utility.BPEL_Activity;
import com.apiit.webserviceclient.utility.BPEL_Process;
import com.apiit.webserviceclient.utility.BPEL_Request;
import com.apiit.webserviceclient.utility.BPEL_Response;
import com.apiit.webserviceclient.utility.GlobalData;

public class Login_Activity extends Activity implements OnClickListener {

	private Authentication_DAO datasource;

	EditText username;
	EditText pass;
	Button button;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login_);
		// findViewById(R.id.textView2);
		// initialDisplayProcess();
		//final Intent i = new Intent(this, OPACSearchResult.class);
		username = (EditText) findViewById(R.id.editTxtLibusername);
		pass = (EditText) findViewById(R.id.EditTxtLibpassword);
		button = (Button) findViewById(R.id.btnLibLogin);
		button.setOnClickListener(this);

		datasource = new Authentication_DAO(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login_, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == button) {
			datasource.open();
			try {
				BPEL_Process bpelProcess = new BPEL_Process();
				BPEL_Request request = new BPEL_Request();
				request.setUrl("http://webspace1.apiit.lk/gims/library/Logon.asp");

				Map<String, String> xpathMap = new HashMap<String, String>();
				xpathMap.put(
						"SID",
						"/html[1]/body[1]/div[1]/table[1]/tr[1]/td[1]/table[1]/tr[4]/td[1]/input[2]---value");
				request.setXpathMap(xpathMap);
				Map<String, String> parametersMap = new HashMap<String, String>();
				request.setParametersMap(parametersMap);
				request.setType("GET");
				BPEL_Response respones = null;

				respones = bpelProcess.doRequest(request);

				BPEL_Request request2 = new BPEL_Request();
				String url_lib_auth = "http://webspace1.apiit.lk/db_authentication/login.asp?REDIRECTPAGE=http://webspace1.apiit.lk/gims/library/Logon.asp&SID="
						+ respones.getResultsMap().get("SID") + "&APPID=";
				request2.setUrl(url_lib_auth);

				Map<String, String> xpathMap2 = new HashMap<String, String>();
				xpathMap2.put("title", "//title---");
				request2.setXpathMap(xpathMap2);
				Map<String, String> parametersMap2 = new HashMap<String, String>();

				parametersMap2
						.put("txtUserName", username.getText().toString());
				parametersMap2.put("txtPassword", pass.getText().toString());
				parametersMap2.put("REDIRECTPAGE",
						"http://webspace1.apiit.lk/gims/library/Logon.asp");
				parametersMap2.put("SID", respones.getResultsMap().get("SID"));
				parametersMap2.put("APPID", "");
				parametersMap2.put("Submit", "Login");

				request2.setParametersMap(parametersMap2);
				request2.setType("POST");
				BPEL_Response respones2 = null;

				respones2 = bpelProcess.doRequest(request2);

				BPEL_Process bpelProcess3 = new BPEL_Process();
				BPEL_Request request3 = new BPEL_Request();
				request3.setUrl("http://webspace1.apiit.lk/gims/library/Links.asp");

				Map<String, String> xpathMap3 = new HashMap<String, String>();
				xpathMap3.put("title", "/html[1]/body[1]/p[4]/a[1]---");
				request3.setXpathMap(xpathMap3);
				Map<String, String> parametersMap3 = new HashMap<String, String>();
				request3.setParametersMap(parametersMap3);
				request3.setType("GET");
				BPEL_Response respones3 = null;

				respones3 = bpelProcess3.doRequest(request3);

				String testResult = respones3.getResultsMap().get("title");

				if (respones3.getStatus().equalsIgnoreCase(
						GlobalData.message_success)) {  //+ Integer.parseInt(username.getText().toString()))
					if (datasource.getAction(GlobalData.authentication_id_lib_perUser ) == null) {
						Authentication authenticaton = new Authentication();
						authenticaton
								.setAuthentication_id(GlobalData.authentication_id_lib_perUser);
						authenticaton
								.setauthentication_name(GlobalData.authentication_name_lib);
						authenticaton.setuser_name(username.getText()
								.toString());
						authenticaton.setpassword(pass.getText().toString());
						datasource.createAction(authenticaton);
					} else {
						Authentication authenticaton = new Authentication();
						authenticaton.setuser_name(username.getText()
								.toString());
						authenticaton.setpassword(pass.getText().toString());
						datasource.updateAuthenticationDetails(
								GlobalData.authentication_id_lib_perUser,
								GlobalData.authentication_name_lib, username.getText().toString(), pass.getText().toString());

					}

					datasource.close();

					Toast.makeText(getApplicationContext(), testResult,
							Toast.LENGTH_LONG).show();
					Intent i = new Intent(this, Library.class);
					i.putExtra("title", testResult);
					startActivity(i);

				} else {
					AlertDialog.Builder alt = new AlertDialog.Builder(this);
					alt.setTitle("Error...");
					alt.setMessage("The username or password you entered is incorrect");
					alt.setNeutralButton("Close", null);
					alt.show();
					Intent i = new Intent(this, Login_Activity.class);
					startActivity(i);

				}

			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
